#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e2 + 10, INF = 0x3f3f3f3f;

int n, m;
vector<pair<int, int>> g[N];
int f[N][N];

void dfs(int x, int fa)
{
    for (auto [y, w] : g[x]) {
        if (y == fa) continue;
        dfs(y, x);
    }
    for (auto [y, w] : g[x]) {
        if (y == fa) continue;
        for (int j = m;j >= 0;j--)
            for (int k = 0;k <= j - 1;k++)
                f[x][j] = max(f[x][j], f[y][k] + f[x][j - k - 1] + w);
    }
}

void solve()
{
    cin >> n >> m;
    for (int i = 1;i < n;i++) {
        int x, y, w; cin >> x >> y >> w;
        g[x].push_back({ y,w });
        g[y].push_back({ x,w });
    }

    dfs(1, -1);
    cout << f[1][m];
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    //cin>>T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}